package com.zhiqingchun.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhiqingchun.common.Page;
import com.zhiqingchun.dao.article.IArticleDao;
import com.zhiqingchun.dao.category.CategoryResult;
import com.zhiqingchun.dao.category.ICategoryDao;
import com.zhiqingchun.pojo.ArticleDO;

@Controller
@RequestMapping("/default.html")
public class DefaultController {

	@RequestMapping(method = RequestMethod.GET )
	public String category(ModelMap map, HttpServletRequest request, HttpServletResponse response, Page page) {
//		Page page = new Page();
		ArticleDO articleDO = new ArticleDO();
		String _id = request.getParameter("id");
		if (_id != null) {
			int id = Integer.parseInt(_id);
			CategoryResult categoryResult = categoryDao.byId(id);
			if (categoryResult.isSuccess()) {
				map.addAttribute("category",  categoryResult.getCategory());
			}
			articleDO.setCategory(id);
		} else {
			//TODO 这里应该抛出一个错误，而不是设为0
			articleDO.setCategory(0);
		}
		List<ArticleDO> list = articleDao.list(articleDO, page);
		map.addAttribute("list", list);
		return "category/category";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String category2(ModelMap map, HttpServletRequest request, HttpServletResponse response, Page page) {
		return category(map, request, response, page);
	}
	
	@Autowired
	private IArticleDao articleDao;
	
	@Autowired
	private ICategoryDao categoryDao;

	public ICategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public IArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(IArticleDao articleDao) {
		this.articleDao = articleDao;
	}
}
